<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>教师管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">教师名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" placeholder="教师名称" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <!--表格头部工具-->
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add">
                    <i class="layui-icon layui-icon-add-circle"></i>添加教师 </button>
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="delBatch">
                    <i class="layui-icon layui-icon-add-circle"></i>批量删除 </button>
            </div>
        </script>
        <!--表格-->
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <!--表格内部工具-->
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
        <!--添加修改页面-->
        <div style="display: none" id="edit">
            <form class="layui-form"  lay-filter="teacher-filter"  >
                <input  type="hidden" value="" name="id">
                <div class="layui-form-item">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-block">
                        <input type="text" value="" placeholder="请输入姓名" name="username" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">毕业院校</label>
                    <div class="layui-input-block">
                        <input type="text" value="" placeholder="请输入毕业院校" name="collage" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">学历</label>
                    <div class="layui-input-block">
                        <input type="text" value="" placeholder="请输入学历" name="grade" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">职称</label>
                    <div class="layui-input-block">
                        <input type="text" value="" placeholder="请输入职称" name="rank" class="layui-input">
                    </div>
                </div>


                <div class="layui-form-item">
                <label class="layui-form-label">性别</label>
                    <div class="layui-input-block">
                    <input type="radio" name="sex" value="男" title="男" checked>
                        <input type="radio" name="sex" value="女" title="女">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">角色名称</label>
                    <div class="layui-input-block">
                        <select name="roleId" placeholder="请选择角色" id="roleId"> <!--动态获取--->
<!--                            <option>&#45;&#45;请选择&#45;&#45;</option>&ndash;&gt;-->
<!--                            <option value="1">李四</option>&ndash;&gt;-->
<!--                            <option value="2">王麻子</option>&ndash;&gt;-->
<!--                            <option value="3">陈鑫</option>&ndash;&gt;-->
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">手机号</label>
                    <div class="layui-input-block">
                        <input type="text" value="" placeholder="请输入手机号" name="phone" class="layui-input">
                    </div>
                </div>


                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','upload'], function () {
        var $ = layui.jquery,
            form = layui.form,
            upload=layui.upload,
            table = layui.table;
        var index;
        //查询角色名称
        $.getJSON("/role/findByParentValue", function(resultData){//响应成功的回调函数
            //  code  请求状态  data 数据 msg错误消息
            if(resultData.code==0){
                // console.log(resultData);//{code:0,msg:成功,data:[{detpId:1,detpName:董事部},{detpId:2,detpName:xx部}]
                // 把数据放到select中
                //生成一段前端元素
                var  option_arr="<option value=''>--请选择--</option>";
                var  data_arr=resultData.data;
                for(var i=0;i<data_arr.length;i++){
                    option_arr+="<option value="+data_arr[i].roleId+">"+data_arr[i].rolename+"</option>\n";
                }
                // console.log(option_arr);
                //但是还没有渲染 select
                $("#roleId").html(option_arr);
                //这是layui的下拉框 有样式修饰所有需要刷新
                form.render('select'); //渲染 select 选择框
            }else{
                layer.msg(resultData.msg);
            }
        });
        //表格的信息字段所在处
        table.render({
            elem: '#currentTableId',
            url: '/teacher/findByPage',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            //
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                // {field: 'id', title: 'ID', width: 80, fixed: 'left', unresize: true, sort: true},
                {field: 'username', title: '姓名', width: 120, edit: 'text'},
                {field: 'sex', title: '性别', width: 120, edit: 'text'},
                {field: 'birthday', title: '生日', width: 120, edit: 'text'},
                {field: 'grade', title: '学历', width: 120, edit: 'text'},
                {field: 'rank', title: '职称', width: 120, edit: 'text'},
                {field: 'jobDate', title: '工作日期', width: 120, edit: 'text'},
                {field: 'collage', title: '毕业院校', width: 120, edit: 'text'},
                // {field: 'password', title: '密码', width: 120, edit: 'text'},
                {field: 'phone', title: '电话号码', width: 120, edit: 'text'},
                // {field: 'roleId', title: '角色ID', width: 120, edit: 'text'},
                {field: 'rolename', title: '角色名称', width: 120, edit: 'text'},
                {title: '操作', minWidth: 140, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10,50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            // console.log(result);
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: data.field

            }, 'data');
            // console.log(result);
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                //把当前隐藏的div进行窗口展示
                var index = layer.open({
                    title: '添加教师',
                    type: 1,
                    shade: 0.2,
                    area: ['600px', '500px'],
                    maxmin:true,
                    content: $("#edit")
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delBatch') {  // 监听删除操作
                console.log("批量删除");
                // layer.alert(JSON.stringify(data));
                //被选中数据批量删除
                //获得被选中的数据
                var checkStatus = table.checkStatus('currentTableId'); //idTest 即为基础参数 id 对应的值
                //获取被选中的数据的个数
                var length=checkStatus.data.length;
                if(length==0){
                    layer.msg("请至少选中一条数据");
                }else {
                    //定义数组 保存所有的id
                    var arrStr = "";
                    var  data=checkStatus.data;//只想获取id的值数组
                    for(var i=0;i<data.length;i++){
                        arrStr+=data[i].id+",";//11,12,13,
                    }
                    arrStr=arrStr.substring(0,arrStr.length-1);
                    console.log(arrStr);

                    // console.log(data);
                    $.post("/teacher/delBatch",{arrStr:arrStr},
                        function(resultData){//后端响应成功后返回的数据结果
                            //  code  请求状态  data 数据 msg错误消息
                            if(resultData.code==0){
                                layer.msg("批量删除成功");
                                //页面自动刷新
                                location.reload();
                            }else{
                                layer.msg("批量删除失败,请联系管理员");
                            }
                            //业务判断
                        }, "json");
                }
            }
        });
        //监听表格里面的工具
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;//获取这一行的数据  被点击的的这一行
            //先给表单赋值  名字不一致一个个赋值
            /* form.val("drug-filter",{
                 "druNo":data.druNo,
                 "druName":data.druName
             });*/
            console.log(data);
            //设置表单所以表单元素的值
            form.val("teacher-filter",data);//
            //img  单独设置
            // layui.$('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', data.druImgPath);
            //修改  打开新的窗口 进行信息编辑
            if (obj.event === 'edit') {
                 index = layer.open({
                    title: '编辑教师',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: $("#edit")
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {//删除的当前
                layer.confirm('真的删除行么', function (index) {
                    console.log(index);
                    //先请求后端做删除操作
                    $.getJSON("/teacher/delById", {id:data.id},
                        function(resultData){//响应成功的回调函数
                            //  code  请求状态  data 数据 msg错误消息
                            console.log(index);
                            if(resultData.code==0){
                                layer.msg("删除成功");
                                //页面自动刷新
                                location.reload();
                                // obj.del();//假删 页面刷新数据回来了
                            }else{
                                layer.msg("删除失败,请联系管理员");
                            }

                        });
                    layer.close(index);
                });
            }
        });
        //修改添加的事件
        form.on('submit(demo1)', function (data) {
            //输出打印表单的字段信息:
            console.log(data.field);
            var  data=data.field;
            //怎么是修改还是添加?
            if(data.id==""){
                console.log("添加");//id自增
                console.log(data);
                //请求后端新增
                $.post("/teacher/save",data,
                    function(resultData){//后端响应成功后返回的数据结果
                        //  code  请求状态  data 数据 msg错误消息
                        console.log(resultData);
                        if(resultData.code==0){
                            layer.msg("添加成功");
                            //页面自动刷新
                            location.reload();
                        }else{
                            layer.msg("添加失败,请联系管理员");
                        }
                        //业务判断
                    }, "json");
            }
            else{
                console.log("修改");//根据id修改
                //请求后端做修改
                $.post("/teacher/update",data,
                    function(resultData){//后端响应成功后返回的数据结果
                        //  code  请求状态  data 数据 msg错误消息
                        console.log(resultData);
                        if(resultData.code==0){
                            layer.msg("修改成功");
                            //页面自动刷新
                            location.reload();
                        }else{
                            layer.msg("修改失败,请联系管理员");
                        }
                    }, "json");
            }
            //关闭当前窗口
            layer.close(index);
            return false;
        });
    });
</script>

</body>
</html>